﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL.Entities;
using BAL;
using System.Collections;

namespace GrouponDesktop
{
    public partial class Main : Form
    {

        private ClienteEntity Cliente;
        private ProveedorEntity Proveedor;
        private UsuarioEntity Usuario;
        private List<FuncionalidadEntity> Funcionalidades;

        //Menu
        private ContextMenuStrip fruitContextMenuStrip;

        public Main(UsuarioEntity user)
        {
            BAL.UsuarioBAL userBal = new UsuarioBAL();

            /*Nos fijamos si es un usuario es un clinete*/
            Usuario = userBal.GetUsuarioByPasswordAndUsername(user);

            //Buscamos las funcionalidades para este usuario
            if (Usuario != null)
            {
                Funcionalidades = userBal.GetFuncionalidesById(Usuario.Rol);
                if (Funcionalidades != null && Funcionalidades.Count > 0)
                {
                    //Armamos el menu    
                    int i = 0;
                    int salto = 0;
                    //Cargamos los items
                    foreach (FuncionalidadEntity item in Funcionalidades)
                    {
                        Button button1 = new Button();
                        button1 = new Button();
                        button1.Text = item.VarEtiqueta;
                        button1.Name = "button" + item.VarNombre;
                        button1.Size = new System.Drawing.Size(75, 50);

                        button1.Location = new System.Drawing.Point(10 + i * 95, 10 + 60 * salto);

                        Controls.AddRange(new System.Windows.Forms.Control[] { button1 });

                        //Asignamos la accion para el boton
                        switch (item.VarNombre)
                        {
                            case "AbmCliente":
                                button1.Click += new System.EventHandler(AbmCliente_Click);
                                break;
                            case "AbmProveedor":
                                button1.Click += new System.EventHandler(AbmProveedor_Click);
                                break;
                            case "ArmarCupon":
                                button1.Click += new System.EventHandler(ArmarCupon_Click);
                                break;
                            case "CargaCredito":
                                button1.Click += new System.EventHandler(CargaCredito_Click);
                                break;
                            case "ComprarCupon":
                                button1.Click += new System.EventHandler(ComprarCupon_Click);
                                break;
                            case "ComprarGiftCard":
                                button1.Click += new System.EventHandler(ComprarGiftCard_Click);
                                break;
                            case "FacturarProveedor":
                                button1.Click += new System.EventHandler(FacturarProveedor_Click);
                                break;
                            case "HistorialCupones":
                                button1.Click += new System.EventHandler(HistorialCupones_Click);
                                break;
                            case "ListadoEstadistico":
                                button1.Click += new System.EventHandler(ListadoEstadistico_Click);
                                break;
                            case "PedirDevolucion":
                                button1.Click += new System.EventHandler(PedirDevolucion_Click);
                                break;
                            case "PublicarCupon":
                                button1.Click += new System.EventHandler(PublicarCupon_Click);
                                break;
                            case "RegistroConsumoCupon":
                                button1.Click += new System.EventHandler(RegistroConsumoCupon_Click);
                                break;
                            case "CambiarPassword":
                                button1.Click += new System.EventHandler(CambiarPassword_Click);
                                break;
                            case "rol":
                                button1.Click += new System.EventHandler(rol_Click);
                                break;
                        }

                        //button1.Click += new System.EventHandler(OnClickButton1);
                        i++;

                        if (i % 5 == 0 && i != 0)
                        {
                            salto++;
                            i = 0;
                        }
                    }
                }
            }

            InitializeComponent();
        }

        private void AbmCliente_Click(object sender, EventArgs e)
        {
            GrouponDesktop.AbmCliente.Form1 abmClienteForm = new GrouponDesktop.AbmCliente.Form1();
            abmClienteForm.ShowDialog();
        }
        private void AbmProveedor_Click(object sender, EventArgs e)
        {
            GrouponDesktop.AbmProveedor.Form1 abmProvForm = new GrouponDesktop.AbmProveedor.Form1();
            abmProvForm.ShowDialog();
        }
        private void ArmarCupon_Click(object sender, EventArgs e)
        {
            GrouponDesktop.ArmarCupon.Form1 ArmarCuponForm = new GrouponDesktop.ArmarCupon.Form1(Usuario.IntIdUsuario);
            ArmarCuponForm.ShowDialog();
        }
        private void CargaCredito_Click(object sender, EventArgs e)
        {
            GrouponDesktop.CargaCredito.CargaCreditoFrm cargaCreditoForm = new GrouponDesktop.CargaCredito.CargaCreditoFrm(Usuario);
            this.Hide();
            cargaCreditoForm.ShowDialog();
            this.Show();
        }

        private void ComprarCupon_Click(object sender, EventArgs e)
        {
            GrouponDesktop.ComprarCupon.ComprarCuponFrm ArmarCuponForm = new GrouponDesktop.ComprarCupon.ComprarCuponFrm(Usuario);
            ArmarCuponForm.ShowDialog();
        }

        private void ComprarGiftCard_Click(object sender, EventArgs e)
        {
            GrouponDesktop.ComprarGiftCard.GiftCardFrm ComprarGiftCardForm = new GrouponDesktop.ComprarGiftCard.GiftCardFrm(Usuario);
            ComprarGiftCardForm.ShowDialog();
        }

        private void FacturarProveedor_Click(object sender, EventArgs e)
        {
            GrouponDesktop.FacturarProveedor.Form1 FacturarProveedorForm = new GrouponDesktop.FacturarProveedor.Form1();
            FacturarProveedorForm.ShowDialog();
        }

        private void HistorialCupones_Click(object sender, EventArgs e)
        {
            GrouponDesktop.HistorialCupones.HistorialCuponesFrm HistorialCuponesForm = new GrouponDesktop.HistorialCupones.HistorialCuponesFrm(Usuario);
            HistorialCuponesForm.ShowDialog();
        }

        private void ListadoEstadistico_Click(object sender, EventArgs e)
        {
            GrouponDesktop.ListadoEstadistico.Form1 ListadoEstadisticoForm = new GrouponDesktop.ListadoEstadistico.Form1();
            ListadoEstadisticoForm.ShowDialog();
        }

        private void PedirDevolucion_Click(object sender, EventArgs e)
        {
            GrouponDesktop.PedirDevolucion.PedirDevolucionFrm PedirDevolucionForm = new GrouponDesktop.PedirDevolucion.PedirDevolucionFrm(Usuario);
            PedirDevolucionForm.ShowDialog();
        }

        private void PublicarCupon_Click(object sender, EventArgs e)
        {
            GrouponDesktop.PublicarCupon.PublicarCupon PublicarCuponForm = new GrouponDesktop.PublicarCupon.PublicarCupon();
            PublicarCuponForm.ShowDialog();
        }

        private void RegistroConsumoCupon_Click(object sender, EventArgs e)
        {
            GrouponDesktop.RegistroConsumoCupon.RegistroConsumoCuponFrm RegistroConsumoCuponForm = new GrouponDesktop.RegistroConsumoCupon.RegistroConsumoCuponFrm(Usuario);
            RegistroConsumoCuponForm.ShowDialog();
        }

        private void CambiarPassword_Click(object sender, EventArgs e)
        {
            GrouponDesktop.CambiarPasswordForm CambiarPasswordForm = new GrouponDesktop.CambiarPasswordForm(Usuario.IntIdUsuario);
            CambiarPasswordForm.ShowDialog();
        }

        private void rol_Click(object sender, EventArgs e)
        {
            GrouponDesktop.AbmRol.Form1 AbmRolForm = new GrouponDesktop.AbmRol.Form1();
            AbmRolForm.ShowDialog();
        }


        // This event handler is invoked when the ContextMenuStrip 
        // control's Opening event is raised. It demonstrates 
        // dynamic item addition and dynamic SourceControl  
        // determination with reuse. 
        void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // Acquire references to the owning control and item.
            Control c = fruitContextMenuStrip.SourceControl as Control;
            ToolStripDropDownItem tsi = fruitContextMenuStrip.OwnerItem as ToolStripDropDownItem;

            // Clear the ContextMenuStrip control's Items collection.
            fruitContextMenuStrip.Items.Clear();

            // Check the source control first. 
            if (c != null)
            {
                // Add custom item (Form)
                fruitContextMenuStrip.Items.Add("Source: " + c.GetType().ToString());
            }
            else if (tsi != null)
            {
                // Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
                fruitContextMenuStrip.Items.Add("Source: " + tsi.GetType().ToString());
            }

            // Populate the ContextMenuStrip control with its default items.
            fruitContextMenuStrip.Items.Add("-");
            fruitContextMenuStrip.Items.Add("Apples");
            fruitContextMenuStrip.Items.Add("Oranges");
            fruitContextMenuStrip.Items.Add("Pears");

            // Set Cancel to false.  
            // It is optimized to true based on empty entry.
            e.Cancel = false;
        }
    }
}
